Skip to content

fix: hierarchical dataflow simulator deadlock and HLS codegen (#561, #565)#577

Open
sunwookim028 wants to merge 4 commits intocornell-zhang:mainfrom
sunwookim028:fix/hierarchical-dataflow-codegen
Open

fix: hierarchical dataflow simulator deadlock and HLS codegen (#561, #565)#577
sunwookim028 wants to merge 4 commits intocornell-zhang:mainfrom
sunwookim028:fix/hierarchical-dataflow-codegen

Conversation

@sunwookim028
Copy link
Copy Markdown
Contributor

Closes #561
Closes #565

Changes

  1. Simulator recursive OMP (allo/backend/simulator.py): build_dataflow_simulator
    only parallelized the top-level function. Inner-region kernel calls ran sequentially,
    deadlocking when kernels communicate via streams. Fix: extracted
    _inject_omp_parallel_sections() and applied it recursively to all functions with PE
    kernel calls.

  2. global_op_cache missing on copy (allo/ir/visitor.py): ASTContext.copy() did
    not copy global_op_cache, causing AttributeError in nested @df.kernel bodies that
    use @stateful variables.

  3. HLS codegen: forward declarations + dataflow pragma (allo/ir/builder.py,
    mlir/lib/Translation/EmitVivadoHLS.cpp): the emitter emitted inner kernel functions
    after the wrapper that calls them, producing "undeclared identifier" errors in Vitis HLS.
    Fix: added a forward-declaration pass before the full emission pass. Also ensures nested
    df.region bodies receive #pragma HLS dataflow. nameTable is cleared between passes
    so type names are re-emitted correctly.
    Note: cherry-picked onto current main after [Fix] several issues related to tests/dataflow/test_hierachical.py #557's emitFunctionSignature refactor;
    conflict resolved by preserving [Fix] several issues related to tests/dataflow/test_hierachical.py #557's SmallVector<Value,8> return type while
    grafting in the forward-declaration loop and nameTable.clear().

Tests

  • tests/dataflow/test_hierachical.py — simulator: PASSED
  • HLS codegen tests require Vitis HLS (brg-zhang-xcel only)

build_dataflow_simulator only injected OMP parallel sections around the
top-level function's func.call ops. Inner region kernel calls remained
sequential, causing deadlock when kernels communicate via streams.
Fix: extract OMP injection into _inject_omp_parallel_sections() helper
and apply it recursively to every function with PE kernel calls, not
just the top function.

Addresses a part of cornell-zhang#561
- Added forward declarations for all functions in the HLS C++ emitter.
- Ensured hierarchical regions are correctly marked with the dataflow attribute.
- Fixed an issue where types were missing in function signatures by clearing the emitter's name table between passes.
sunwookim028 added a commit to sunwookim028/allo that referenced this pull request Apr 13, 2026
- STATE.md: mark PR cornell-zhang#577 CI as PASSED, record stateful→Stateful and
  project structure changes in Completed This Cycle
- ISSUE-003: update status to NEEDS-REVIEW (CI PASSED)
- ISSUE-004: record alias removal (e665bbc) superseding temp alias (5595fab)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@Fangtangtang Fangtangtang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes! I just have some suggestions.

Also, could you add some tests to cover these fixes? It looks like they are addressing issues in hierarchical designs.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does #557 still fail to fix the forward reference issue?

Comment thread allo/ir/builder.py
Comment on lines +2743 to +2744
if ctx.top_func is not None:
func_op.operation.move_before(ctx.top_func)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is also added to fix foward reference. Forward reference is valid in mlir but invalide in HLS backend. We'd better fix the issue in HLS backend codegen.
Could you please check whether existing fix (introduced in #557 ) already works?

Comment thread allo/ir/builder.py
Comment on lines +2752 to +2754
if isinstance(res.type, MemRefType) and len(res.type.shape) == 0:
op_ = ASTTransformer.build_scalar(ctx, arg)
arg_values.append(op_.result)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would pass the scalar argument by value. Is this the intended behavior?

Comment thread allo/ir/builder.py
Comment on lines +2219 to +2225
# If it's a 0D memref (scalar), load it to get the value
if (
isinstance(res.type, MemRefType)
and len(res.type.shape) == 0
):
op_ = ASTTransformer.build_scalar(ctx, arg)
arg_values.append(op_.result)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would pass the scalar argument by value.
Also, I think args should only contain tensors (please correct me if I'm missing something). In that case, we should raise an error in the type infer phase if the user use a scalar here.

Comment thread allo/ir/builder.py
Comment on lines 1993 to 1994
if not hasattr(ctx, "global_op_cache"):
ctx.global_op_cache = {}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove this since hasattr(ctx, "global_op_cache") is always True after the update in ASTContext

Comment thread allo/backend/simulator.py
Comment on lines +772 to +773
if all_pe_calls_by_func is not None and pe_call_define_ops:
all_pe_calls_by_func[func_name] = pe_call_define_ops
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always not None?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants